// -------------------------------------------------------------------------
// Copyright (c) 2000-2009 Ufinity. All Rights Reserved.
//
// This software is the confidential and proprietary information of
// Ufinity
//
// Original author: lxt
//
// -------------------------------------------------------------------------
// UFINITY MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
// THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE, OR NON-INFRINGEMENT. UFINITY SHALL NOT BE
// LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
// MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
//
// THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
// CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
// PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
// NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
// SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
// SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
// PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). UFINITY
// SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
// HIGH RISK ACTIVITIES.
// -------------------------------------------------------------------------

package com.ufinity.estimation.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang.xwork.StringUtils;

import com.cn.ufinity.common.log.LoggerFactory;
import com.ufinity.estimation.persistence.domain.ProjectListing;
import com.ufinity.estimation.service.ProjectListingService;
import com.ufinity.estimation.site.controller.formbean.ProjectListingBean;

/**
 * Description of the class
 * 
 * @author lxt
 * @version 1.0
 * @since 2009-12-14
 */

public class ProjectListingServiceImpl extends BaseServiceImpl<ProjectListing, Long> implements ProjectListingService {
    public ProjectListingServiceImpl() {
        super(LoggerFactory.getPersistenceLog(ProjectListingServiceImpl.class),
                ProjectListing.class);
    }

    @Override
    public void saveOrUpdateEntity(Long projectId,
            Map<Long, ProjectListingBean> projectListingBeans,
            List<ProjectListingBean> newProjectListingBeans) {
        String method = "saveOrUpdateEntity";
        String hql = "from ProjectListing pl where pl.projectId = ?";
        List<ProjectListing> result = getHibernateTemplate().find(hql,
                projectId);

        Map<Integer, Long> phsIds = new HashMap<Integer, Long>();
        for (ProjectListing pl : result) {

            ProjectListingBean plb = projectListingBeans.get(pl.getId());
            if (plb != null) {
                wrapProjectListing(plb, pl);
                updateEntity(pl);
            } else {
                deleteEntity(pl.getId());
            }
        }

        for (int i = 0; newProjectListingBeans != null && i < newProjectListingBeans.size(); i++) {
            ProjectListingBean pdb = newProjectListingBeans.get(i);
            if (pdb == null) {
                continue;
            }
            ProjectListing pl = wrapProjectListing(pdb, null);
            pl.setProjectId(projectId);
            saveEntity(pl);
        }

    }

    private ProjectListing wrapProjectListing(ProjectListingBean plb,
            ProjectListing pl) {
        if (pl == null) {
            pl = new ProjectListing();
        }

        if (plb.getAmount() != null) {
            pl.setAmount(Float.parseFloat(plb.getAmount()));
        }
        pl.setBrand(plb.getBrand());
        pl.setGrade(plb.getGrade());
        pl.setMadeIn(plb.getMadeIn());
        pl.setName(plb.getName());
        pl.setRemark(plb.getRemark());
        pl.setSpecification(plb.getSpecification());
        if (StringUtils.isBlank(plb.getSuggestPrice())) {
            pl.setSuggestPrice(0F);
        } else {
            pl.setSuggestPrice(NumberUtils.toFloat(plb.getSuggestPrice()));
        }
        pl.setUnit(plb.getUnit());
        
        return pl;
    }
}
